/**
 * 
 */
package com.shiro.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shiro.mapper.PermissionMapper;
import com.shiro.mapper.RolePermissionMapper;
import com.shiro.pojo.Permission;
import com.shiro.pojo.PermissionExample;
import com.shiro.pojo.Role;
import com.shiro.pojo.RolePermission;
import com.shiro.pojo.RolePermissionExample;
import com.shiro.service.PermissionService;
import com.shiro.service.RoleService;

/**
 * @author zyj
 *
 */
@Service
public class PermissionServiceImpl implements PermissionService {
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private RolePermissionMapper rolePermissionMapper;

	@Autowired
	private PermissionMapper permissionMapper;
	
	/* 
	 *   �����û�����ѯ�û�������Ȩ��
	 */
	@Override
	public Set<String> listPermissions(String userName) {
		Set<String> result = new HashSet<>();
		List<Role> roles = roleService.listRoles(userName);
		List<RolePermission> rolePermissions = new ArrayList<>();
		for (Role role : roles) {
			RolePermissionExample example = new RolePermissionExample();
			example.createCriteria().andRidEqualTo(role.getId());
			List<RolePermission> rps = rolePermissionMapper.selectByExample(example);
			rolePermissions.addAll(rps);
		}
		for (RolePermission rolePermission : rolePermissions) {
			Permission p = permissionMapper.selectByPrimaryKey(rolePermission.getId());
			result.add(p.getName());
		}
		return result;
	}

	/* 
	 *  ��ѯϵͳ��������ԴȨ��
	 */
	@Override
	public List<Permission> list() {
		PermissionExample example = new PermissionExample();
		example.setOrderByClause("id desc");
		return permissionMapper.selectByExample(example);
	}

	/* 
	 *  ������ԴȨ��
	 */
	@Override
	public void add(Permission permission) {
		permissionMapper.insert(permission);
	}

	/* 
	 * ɾ����Դ
	 */
	@Override
	public void delete(Long id) {
		permissionMapper.deleteByPrimaryKey(id);
	}

	/* 
	 * ��ȡ��Դ
	 */
	@Override
	public Permission get(Long id) {
		return permissionMapper.selectByPrimaryKey(id);
	}

	/* 
	 * ������Դ
	 */
	@Override
	public void update(Permission permission) {
		permissionMapper.updateByPrimaryKeySelective(permission);
	}

	/* 
	 * ĳ����ɫ����Դ
	 */
	@Override
	public List<Permission> list(Role role) {
		List<Permission> result = new ArrayList<>();
		RolePermissionExample example = new RolePermissionExample();
		example.createCriteria().andRidEqualTo(role.getId());
		List<RolePermission> rps = rolePermissionMapper.selectByExample(example);
		for(RolePermission rolePermission:rps) {
			result.add(permissionMapper.selectByPrimaryKey(rolePermission.getPid()));
		}
		return result;
	}

	/* 
	 * 
	 */
	@Override
	public boolean needInterceptor(String requestURI) {
		List<Permission> ps = list();
		for (Permission permission : ps) {
			if(permission.getUrl().equals(requestURI)) {
				return true;
			}
		}
		return false;
	}

	/* 
	 * 
	 */
	@Override
	public Set<String> listPermissionURLs(String userName) {
		Set<String> result = new HashSet<>();
        List<Role> roles = roleService.listRoles(userName);
 
        List<RolePermission> rolePermissions = new ArrayList<>();
 
        for (Role role : roles) {
            RolePermissionExample example = new RolePermissionExample();
            example.createCriteria().andRidEqualTo(role.getId());
            List<RolePermission> rps = rolePermissionMapper.selectByExample(example);
            rolePermissions.addAll(rps);
        }
 
        for (RolePermission rolePermission : rolePermissions) {
            Permission p = permissionMapper.selectByPrimaryKey(rolePermission.getPid());
            result.add(p.getUrl());
        }
 
        return result;
	}

}
